package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.admin.vo.UpdateGoodsVo;
import com.cskaoyan.bean.admin.vo.CatAndBrandVo;
import com.cskaoyan.bean.admin.vo.CreateGoodsVo;
import com.cskaoyan.bean.admin.vo.GoodsDetailVo;
import com.cskaoyan.service.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("admin/goods")
public class GoodsManagementController {

    @Autowired
    GoodsService goodsService;

    @RequiresPermissions("admin:goods:list")
    @RequestMapping("/list")
    public BaseRespVo GoodsList(Integer goodsId , String goodsSn , String name , BaseParam param){

        BasePageData basePageData = goodsService.selectGoodsCount(goodsId , goodsSn , name , param);

        return BaseRespVo.ok(basePageData);
    }

    @RequiresPermissions("admin:goods:create")
    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand(){
        CatAndBrandVo catAndBrandVo = goodsService.selectCategory();

        return BaseRespVo.ok(catAndBrandVo);
    }

    @RequiresPermissions("admin:goods:create")
    @PostMapping ("create")
    public BaseRespVo create(@RequestBody CreateGoodsVo createGoodsVo){

        goodsService.addGoods(createGoodsVo);
        return BaseRespVo.ok();
    }

//    @RequiresPermissions("admin:goods:read")
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){

        GoodsDetailVo goodsDetailVo = goodsService.selectDetail(id);

        return BaseRespVo.ok(goodsDetailVo);
    }

    @RequiresPermissions("admin:goods:update")
    @PostMapping("update")
    public BaseRespVo update(HttpServletRequest req, @RequestBody UpdateGoodsVo goods){

        goodsService.updateGoods(req, goods);


        return BaseRespVo.ok();
    }

    @RequiresPermissions("admin:goods:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Goods goods){
        goodsService.deleteGoods(goods);

        return BaseRespVo.ok();
    }
}
